package com.zyark.web.util;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Project Name: wo-shop .<br>
 * Author: Ku_ker .<br>
 * File Name: SHA1Utils.java .<br>
 * Package Name: com.zyark.web.util .<br>
 * Date: 2017年5月17日下午3:58:53 .<br>
 * Copyright (c) 2017, 952630623@qq.com All Rights Reserved .<br>
 *
 */

/**
 * ClassName: SHA1Utils .<br/>
 * Function: .<br/>
 * Reason: .<br/>
 * date: 2017年5月17日 下午3:58:53 .<br/>
 *
 * @author Ku_ker
 * @version 1.0
 * @JDK 1.8
 */

public class SHA1Utils {

	/**
	 * 
	 * SHA1作用: SHA、SHA1加密. <br/>
	 * 适用条件：.<br/>
	 * 
	 * @author Ku_ker
	 * @param str
	 *            待加密字符串
	 * @return 加密后的字符串
	 * @JDK 1.8
	 * @since 1.0
	 */
	public static String SHA1(String str) {
		try {
			MessageDigest digest = MessageDigest.getInstance("SHA-1"); // 如果是SHA加密只需要将"SHA-1"改成"SHA"即可
			digest.update(str.getBytes());
			byte messageDigest[] = digest.digest();
			// Create Hex String
			StringBuffer hexStr = new StringBuffer();
			// 字节数组转换为 十六进制 数
			for (int i = 0; i < messageDigest.length; i++) {
				String shaHex = Integer.toHexString(messageDigest[i] & 0xFF);
				if (shaHex.length() < 2) {
					hexStr.append(0);
				}
				hexStr.append(shaHex);
			}
			return hexStr.toString();

		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}
		return null;
	}

}
